//
//  SearchViewController.m
//  LoverGo
//
//  Created by liudonghuan on 14-9-19.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "ShangJiaViewController.h"
#import "LGUtil.h"
@interface SearchViewController ()

@end

@implementation SearchViewController


-(id)init
{
    self = [super init];
    if (self) {
    }
    return self;
}
-(void)loadView{
    [self dinglan];
    _reciveArr = [[NSMutableArray alloc]init];
    _mysearchview = [[SearchView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _mysearchview.mysearchBar.delegate = self;
    _mysearchview.mytableView.delegate = self;
    _mysearchview.mytableView.dataSource = self;
    _mysearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_mysearchview.mysearchBar contentsController:self];
    _mysearchDisplayController.delegate = self;
    [_mysearchDisplayController setSearchResultsDataSource:self];
    [_mysearchDisplayController setSearchResultsDelegate:self];
    
    [self setView:_mysearchview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == _mysearchDisplayController.searchResultsTableView){
        return _reciveArr.count;
    }else{
        return 0;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_reciveArr.count > 0) {
        NSMutableDictionary*  dictleft = _reciveArr[indexPath.row];
        NSString *para = [dictleft objectForKey:@"uuid"];
        ShangJiaViewController *_shangJiaViewController = [[ShangJiaViewController alloc]initWithDic:para];
        self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
        [self.navigationController pushViewController:_shangJiaViewController animated:YES];
    }
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchTableViewCell *cell = [[SearchTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    if(tableView == _mysearchDisplayController.searchResultsTableView){
        NSMutableDictionary*  dict = _reciveArr[indexPath.row];
        cell.searchtitle.text = [dict objectForKey:@"placeName"];
        
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    [self getData:searchText];
}
-(void)getData:(NSString *)value
{
    NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:
                          value,@"searchInfo",
                          @"tj",@"city",
                          nil];
    
    [[RequestManager getSingleton] sendRequest:searchPlace para:para callBack:^(id responseObj) {
        if ([[responseObj objectForKey:@"status"] intValue] == 0) {
            _reciveArr = [responseObj objectForKey:@"data"];
            [_mysearchDisplayController.searchResultsTableView reloadData];
        }
    }];
    
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
	/*
     Bob: Because the searchResultsTableView will be released and allocated automatically, so each time we start to begin search, we set its delegate here.
     */
	[_mysearchDisplayController.searchResultsTableView setDelegate:self];
    
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
	
    [_mysearchview.mysearchBar resignFirstResponder];
}

-(void)dinglan{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = @"搜索";
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"搜索"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"搜索"];
}

@end
